import scrapy
from robot.items import Novel, Chapter
from robot.items import Torrent


class BtsoSpider(scrapy.Spider):
    name = 'btso'

    start_urls = ['https://www.sosobttt.com/s/xart/1.html']

    def parse(self, response):
        for href in response.css('.r .link::attr(href)'):
            yield response.follow(href, self.parse_book)

        for href in response.css('.next a::attr(href)'):
            yield response.follow(href, self.parse)

    def parse_author(self, response):
        def extract_with_css(query):
            return response.css(query).extract_first().strip()

        yield {
            'name': extract_with_css('h3.author-title::text'),
            'birthdate': extract_with_css('.author-born-date::text'),
            'bio': extract_with_css('.author-description::text'),
        }

    def parse_book(self, res):
        files = []
        for li in res.css('#filelist li'):
            files.append({
                    'name':li.xpath('span').extract_first(),
                    'size':None
                })

        meta_list = {}
        for li in res.css('#content ul li'):
            print(li.text())
            m = li.xpath('text()').extract_first().strip().split(':')
            meta_list[m[0]] = m[1]

        t = Torrent({
            'title': res.css('h1::text').extract_first().strip(),
            'files': json.dumps(files)
            })
        t.update(meta_list)
        yield t
